<template>
  <div class="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in imgs" :key= item.image>
       <img :src="item.image" :alt="item.title" width="300px" height="150px">
      </div>
      
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

    <!-- 如果需要滚动条 -->
    <!-- <div class="swiper-scrollbar"></div> -->
  </div>
</template>

<script>
import "swiper/css/swiper.css"; //引入swiper样式
import Swiper from "swiper"; //引入swiper
import { getImgList } from "@/request/api.js";
export default {
  name: "Swiper",
  data() {
    return {
      imgs: [],
    };
  },
  mounted() {
    getImgList().then(
      (response) => {
        this.imgs = response.data;
      },
      (error) => {
        alert(error);
      }
    );
  },
  watch: {
    imgs: {
      immediate: true,
      handler(newVal) {
        this.$nextTick(() => {
          var mySwiper = new Swiper(".swiper", {
            direction: "horizontal", // 垂直切换选项
            loop: true, // 循环模式选项
            autoplay: true, //自动轮播

            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
              clickable: true, //分页器是否可以点击
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },

            // 如果需要滚动条
            //   scrollbar: {
            //     el: ".swiper-scrollbar",
            //   },
          });
        });
      },
    },
  },
};
</script>

<style>
.swiper {
  width: 300px;
  height: 150px;
  overflow: hidden;
  position: relative;
}
</style>
